<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">HIS医疗信息管理系统</div>
            <el-form :model="param" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="logName">
                    <el-input v-model="param.logName" placeholder="请输入用户名">
                        <el-button slot="prepend" icon="iconfont icon-user-group-fill"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="logPwd">
                    <el-input
                        type="password"
                        placeholder="请输入密码"
                        v-model="param.logPwd"
                    >
                        <el-button slot="prepend" icon="iconfont icon-lock-fill"></el-button>
                    </el-input>
                </el-form-item>

                <!--验证码-->
                <el-form-item prop="verifyCode">

                    <el-row :gutter="8">
                        <el-col :span="14">
                            <el-input
                                    type="text"
                                    placeholder="请输入验证码"
                                    v-model="param.verifyCode"
                                    @keyup.enter.native="submitForm()"
                            >
                                <el-button slot="prepend" icon="iconfont icon-picture-fill"></el-button>
                            </el-input>
                        </el-col>
                        <el-col :span="10">
                            <el-image
                                    style="width: 144px; height: 40px; cursor:pointer;"
                                    :src="verifySrc"
                                    fit="fill"
                                    @click="changeImg"></el-image>
                        </el-col>
                    </el-row>
                </el-form-item>

                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>

export default {
    data: function() {
        return {
            param: { //提交的表单
                logName: 'admin',
                logPwd: 'admin',
                verifyCode:'', //验证码
                key:'' //redis key
            },
            rules: {
                logName: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                logPwd: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                verifyCode:[{ required: true, message: '请输入验证码', trigger: 'blur' }]
            },
            verifySrc:''
        };
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    //提交登录表单，调用后端登录方法
                    this.$api.user.login(this.param).then(res=>{
                        //res.code==200 ，进入，跳转到主页
                        if(res.code === 200){
                            this.$router.push('/welcome');
                        }else{
                            this.changeImg();
                        }
                    });
                } else {
                    this.$message.error('请完善信息');
                    return false;
                }
            });
        },
        changeImg(){
            this.$api.user.captcha().then(res=>{
                //验证码存储在 param
                this.param.key = res.data.key;
                //替换图片
                this.verifySrc = res.data.img;
            });
        }
    },
    mounted() {
        this.changeImg();
    }
};
</script>
<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url('./../assets/img/login-bg.jpg');
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 24px;
    font-weight: 900;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 420px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.login-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>